/*
 * @Author: your name
 * @Date: 2021-04-01 16:55:02
 * @LastEditTime: 2021-04-16 11:12:27
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \web\node\route\sowing.js
 */
import express from "express";
import Sowing from "./../models/Sowing";
import formidable from "formidable";
import config from "./../src/config";
import { basename } from "path";

const route = express.Router({});

/**************************** 接口API *************************/
/* 往数据库中插入一条新数据 */
route.post("/back/sowing/api/add", (req, res, next) => {
  const form = new formidable.IncomingForm();
  form.uploadDir = config.uploadsPath;
  form.keepExtensions = true;
  form.parse(req, (err, fields, files) => {
    if (err) {
      return next(err);
    }
    let body = fields;
    body.image_url = basename(files.image_url.path);
    const sowing = new Sowing({
      // 图片名称
      image_title: body.image_title,
      // 图片地址
      image_url: body.image_url,
      // 跳转链接
      image_link: body.image_link,
      // 上架时间
      s_time: body.s_time,
      // 下架时间
      e_time: body.e_time,
    });

    sowing.save((err, result) => {
      if (err) {
        return next(err);
      }
      res.json({
        status: 200,
        result: "添加轮播图成功",
      });
    });
  });
});

/* 查询所有轮播图数据 */
route.get("/back/sowing/api/list", (req, res, next) => {
  Sowing.find(
    {},
    "_id image_title image_url  image_link s_time e_time",
    (err, docs) => {
      if (err) {
        return next(err);
      }
      res.json({
        status: 200,
        result: docs,
      });
    }
  );
});

/* 根据id查询一条轮播图数据 */
route.get("/back/sowing/api/single/:sowingId", (req, res, next) => {
  Sowing.find(
    { _id: req.params.sowingId },
    "_id image_title image_url  image_link s_time e_time",
    (err, docs) => {
      if (err) {
        return next(err);
      }
      res.json({
        status: 200,
        result: docs,
      });
    }
  );
});

/* 根据id去修改一条轮播图数据 */
route.post("/back/sowing/api/edit", (req, res, next) => {

  const form = new formidable.IncomingForm();
  form.uploadDir = config.uploadsPath;
  form.keepExtensions = true;
  form.parse(req, (err, fields, files) => {
    if (err) {
      return next(err);
    }
    let body = fields;

    Sowing.findById(body.id,(err,sowing)=>{
 
      // 图片名称
      sowing.image_title = body.image_title,
      // 图片地址
      sowing.image_url = body.image_url || basename(files.image_url.path),
      // 跳转链接
      sowing.image_link = body.image_link,
      // 上架时间
      sowing.s_time = body.s_time,
      // 下架时间
      sowing.e_time = body.e_time,
      //最后编辑时间
      sowing.l_time = new Date()

  
      sowing.save((err, result) => {
        if (err) {
          return next(err);
        }
        console.log(result);
        res.json({
          status: 200,
          result: "修改轮播图成功",
        });
      });
    })
    
  });
});

/* 根据id去删除一条轮播图数据 */
route.get("/back/sowing/api/delete/:sowingId", (req, res, next) => {
  Sowing.deleteOne({ _id: req.params.sowingId }, (err, result) => {
    if (err) {
      return next(err);
    }
    if (result.ok === 1) {
      res.json({
        status: 200,
        result: "轮播图删除成功！",
      });
    }
  });
});


/**************************** 页面路由 *************************/

/* 加载轮播图管理页面 */
route.get("/back/s_list", (req, res, next) => {
  Sowing.find((err, sowings) => {
    if (err) {
      return next(err);
    }
    res.render("back/sowing_list.html", { sowings });
  });
});

/* 加载添加轮播图界面 */
route.get("/back/s_add", (req, res, next) => {
  res.render("back/sowing_add.html");
});

/* 加载修改轮播图界面 */
route.get("/back/s_edit", (req, res, next) => {
  res.render("back/sowing_edit.html");
});


export default route;
